$(function(){
    //背景原子效果
    particlesJS.load('particles-js', '/static/js/sign_particles.json', function() {
        //console.log('callback - particles.js config loaded');
    });
});

//pjax
document.addEventListener('pjax:send', function() {
    NProgress.start();
});

document.addEventListener('pjax:complete', function() {
    NProgress.done();
});

document.addEventListener('pjax:error', function() {

});

document.addEventListener('pjax:success', function() {

});

document.addEventListener('DOMContentLoaded', function() {
    var pjax = new Pjax({
        elements: 'a.pjax',
        selectors: [
            'title',
            '#wrapper',
            '#mini_topbar'
        ]
    });
});

//登录
function signin(){
    var $email = $('#email');
    var $pwd = $('#pwd');
    var $remember_me = $('#remember_me');
    var email = $email.val();
    var pwd = $pwd.val();
    var remember_me = 0;
    if($remember_me.is(':checked')){
        remember_me = 1;
    }

    if(!simple_validate.required(email)){
        layer.msg('请填写邮箱');
        return false;
    }

    if(!simple_validate.email(email)){
        layer.msg('请填写正确的邮箱');
        return false;
    }

    if(!simple_validate.range(email, 0, 30)){
        layer.msg('邮箱最大长度为30');
        return false;
    }

    if(!simple_validate.required(pwd)){
        layer.msg('请填写密码');
        return false;
    }

    if(!simple_validate.range(pwd, 6, 16)){
        layer.msg('密码长度为6-16位');
        return false;
    }

    var post_data = {
        email: email,
        pwd: pwd,
        remember_me: remember_me
    };
    //Geetest验证码
    var $captcha = $('#captcha');
    if($captcha.length > 0){
        post_data.geetest_challenge = $(':input[name=geetest_challenge]').val();
        post_data.geetest_validate = $(':input[name=geetest_validate]').val();
        post_data.geetest_seccode = $(':input[name=geetest_seccode]').val();
    }

    layer.load();
    $.post(
        '/baseapi/account/signin',
        post_data,
        function(json){
            if(json.error_code == 'ok'){
                document.location = ref;
            }
            else{
                show_error(json.error_code);
                layer.closeAll('loading');
            }
        },
        'json'
    );

    return false;
}

//注册
function signup(){
    var $email = $('#email');
    var $pwd = $('#pwd');
    var $nickname = $('#nickname');
    var email = $email.val();
    var pwd = $pwd.val();
    var nickname = $nickname.val();

    if(!simple_validate.required(email)){
        layer.msg('请填写邮箱');
        return false;
    }

    if(!simple_validate.email(email)){
        layer.msg('请填写正确的邮箱');
        return false;
    }

    if(!simple_validate.range(email, 0, 30)){
        layer.msg('邮箱最大长度为30');
        return false;
    }

    if(!simple_validate.required(pwd)){
        layer.msg('请填写密码');
        return false;
    }

    if(!simple_validate.range(pwd, 6, 16)){
        layer.msg('密码长度为6-16位');
        return false;
    }

    if(!simple_validate.required(nickname)){
        layer.msg('请填写昵称');
        return false;
    }

    if(!simple_validate.nickname(nickname)){
        layer.msg('昵称只能为中文、字母、数字或横线-');
        return false;
    }

    if(!simple_validate.mix_range(nickname, 1, 16)){
        layer.msg('昵称长度为1-16位，1个中文算2个字符长度');
        return false;
    }

    var post_data = {
        email: email,
        pwd: pwd,
        nickname: nickname
    };
    //Geetest验证码
    var $captcha = $('#captcha');
    if($captcha.length > 0){
        post_data.geetest_challenge = $(':input[name=geetest_challenge]').val();
        post_data.geetest_validate = $(':input[name=geetest_validate]').val();
        post_data.geetest_seccode = $(':input[name=geetest_seccode]').val();
    }

    layer.load();
    $.post(
        '/baseapi/account/signup',
        post_data,
        function(json){
            if(json.error_code == 'ok'){
                //发送注册验证邮件
                document.location = '/account/signup_email_send/send';
            }
            else{
                show_error(json.error_code);
                layer.closeAll('loading');
            }
        },
        'json'
    );

    return false;
}